package com.david.apipassenger.gray;

import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 拦截器
 */
@Aspect
@Component
public class RequestAspect {

    //过滤的方式：包名、类名、返回值、方法
    @Pointcut("execution(* com.david.apipassenger.controller..*Controller*.*(..))")
    private void anyMehtod(){

    }

    @Before(value = "anyMehtod()")
    public void before(JoinPoint joinPoint){

        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String version = request.getHeader("version");

        //之前的版本
//        Map<String,String> map=new HashMap<>();
//        map.put("version",version);
//        RibbonParameters.set(map);

        // 灰度规则 匹配的地方 查db，redis ====
        if (version.trim().equals("v2")){
            RibbonFilterContextHolder.getCurrentContext().add("version","v2");
        }





    }
}
